package gui.ClassroomMenu;

import gui.ClassroomMenu.exceptions.NoData;
import gui.ClassroomMenu.exceptions.NoDataException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.BorderFactory;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;

import model.basics.ClassRoom;
import model.basics.Resource;
import org.jdesktop.application.Application;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModifClassroom extends javax.swing.JPanel {

private JButton cButton;
private JComboBox statusComboBox;
private JTextField mcTextField;
private JTextField idTextField;
private JTextField nameTextField;
private JLabel statusLabel;
private JLabel mcLabel;
private JLabel idLabel;
private JLabel nameLabel;
private JButton okButton;
private AddResIntoClassroom resSPanel;
private JList crList;
private JFrame frame;
private ClassRoom cr;
private HashMap<Resource, Integer> crhm;
private Boolean crop;
	
	public ModifClassroom(JList classroomList, ClassRoom classRoom) {
		super();
		this.cr = classRoom;
		this.crhm = cr.getResources();
		this.crop = cr.getOperationalStatus();
		this.crList = classroomList;
		this.setPreferredSize(new java.awt.Dimension(671, 303));
		initGUI();
	}

	private void initGUI() {
		frame = new JFrame();
		frame.getContentPane().add(this);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setTitle("Modify a classroom");
		frame.setVisible(true);
		try {
			this.setLayout(null);
			this.setOpaque(false);
			{
				okButton = new JButton();
				this.add(okButton);
				okButton.setText("OK");
				okButton.setBounds(33, 249, 89, 21);
				okButton.addActionListener(new ModifCR());
			}
			{
				nameLabel = new JLabel();
				this.add(nameLabel);
				nameLabel.setText("Name");
				nameLabel.setBounds(12, 30, 113, 14);
			}
			{
				idLabel = new JLabel();
				this.add(idLabel);
				idLabel.setText("Id");
				idLabel.setBounds(12, 83, 113, 14);
			}
			{
				mcLabel = new JLabel();
				this.add(mcLabel);
				mcLabel.setText("Maximun Capacity");
				mcLabel.setBounds(12, 128, 113, 14);
			}
			{
				statusLabel = new JLabel();
				this.add(statusLabel);
				statusLabel.setText("Status");
				statusLabel.setBounds(12, 172, 113, 14);
			}
			{
				nameTextField = new JTextField();
				this.add(nameTextField);
				nameTextField.setBounds(125, 27, 149, 21);
				nameTextField.setText(cr.getName());
			}
			{
				idTextField = new JTextField();
				this.add(idTextField);
				idTextField.setBounds(125, 76, 149, 21);
				idTextField.setText(((Integer)cr.getId()).toString());
			}
			{
				mcTextField = new JTextField();
				this.add(mcTextField);
				mcTextField.setBounds(125, 121, 149, 21);
				mcTextField.setText(((Integer)cr.getMaxCapacity()).toString());
			}
			{
				ComboBoxModel statusComboBoxModel;
				if(crop)
					statusComboBoxModel = 
						new DefaultComboBoxModel(
							new String[] { "Operational", "Non Operational" });
				else
					statusComboBoxModel = 
						new DefaultComboBoxModel(
							new String[] { "Non Operational","Operational" });
				
				statusComboBox = new JComboBox();
				this.add(statusComboBox);
				statusComboBox.setModel(statusComboBoxModel);
				statusComboBox.setBounds(125, 165, 149, 21);
			}
			{
				resSPanel = new AddResIntoClassroom(crhm);
				this.add(resSPanel);
				resSPanel.setBounds(286, 12, 373, 279);
				resSPanel.setBorder(BorderFactory.createTitledBorder("Resources"));
			}
			{
				cButton = new JButton();
				this.add(cButton);
				cButton.setBounds(163, 249, 89, 21);
				cButton.setName("cButton");
				cButton.setText("Cancel");
				cButton.setToolTipText("Back to Classroom menu");
				cButton.addActionListener(new QuitCR());
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public class ModifCR implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			try {
				if(nameTextField.getText().isEmpty() | idTextField.getText().isEmpty() | mcTextField.getText().isEmpty())
					throw new NoDataException();
				else{
					int id=Integer.parseInt(idTextField.getText());
					int maxCapacity = Integer.parseInt(mcTextField.getText());
					
					ClassRoom crResult = new ClassRoom(id, maxCapacity, nameTextField.getText());
					crResult.setResources(resSPanel.getHmres());
					boolean op = ((String)statusComboBox.getSelectedItem()).equals("Operational");
					crResult.changeOperationalStatus(op);
					
					model.search.System.getClassRoomGeneralList().remove(cr);
					model.search.System.getClassRoomGeneralList().add(crResult);
					crList.setListData((Vector<ClassRoom>)model.search.System.getClassRoomGeneralList());
					frame.dispose();
				}
			} catch (NoDataException e) {
				new NoData();
					}
		}
	}
	
	public class QuitCR implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			frame.dispose();
		}
	}
}
